package com.billard.controller;

import com.billard.service.ReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * 报表控制器
 */
@RestController
@RequestMapping("/report")
@Tag(name = "报表管理", description = "报表导出相关接口")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @GetMapping("/daily-revenue")
    @PreAuthorize("@securityPermission.hasPermission('report:dailyRevenue')")
    @Operation(summary = "导出每日营收报表", description = "导出指定日期的营收报表")
    public void exportDailyRevenueReport(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
            HttpServletResponse response) { // ✅ 使用正确
        reportService.exportDailyRevenueReport(date, response);
    }

    @GetMapping("/consumption")
    @PreAuthorize("@securityPermission.hasPermission('report:consumption')")
    @Operation(summary = "导出消费记录报表", description = "导出指定时间范围的消费记录报表")
    public void exportConsumptionReport(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            HttpServletResponse response) {
        reportService.exportConsumptionReport(startDate, endDate, response);
    }

    @GetMapping("/table-usage")
    @PreAuthorize("@securityPermission.hasPermission('report:tableUsage')")
    @Operation(summary = "导出球台使用统计报表", description = "导出指定时间范围的球台使用统计报表")
    public void exportTableUsageReport(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            HttpServletResponse response) {
        reportService.exportTableUsageReport(startDate, endDate, response);
    }
}
